<template>
  <div>
    <el-dialog title="分发设置" :visible.sync="isDistribution" width="60%" :before-close="handleClose" :close-on-click-modal='false'>
      <p>说明：每次分发组合和量表都自动创建“测评计划”，一个测评计划生成一份团队报告</p>
      <el-form ref="form" :model="form" label-width="80px" label-position="left">
        <el-form-item label="分发范围">
          {{form.real_name}}
        </el-form-item>
        <el-form-item label="测评量表">
          <el-button type="primary" size="mini" @click="addPlanFn">添加测评计划</el-button>
        </el-form-item>
      </el-form>
      <simple-table :tableConfig="tableConfig" :tableData="tableData"></simple-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleClose">确 定</el-button>
      </span>
    </el-dialog>
    <add-plan v-if="isAddPlan" :isAddPlan="this.isAddPlan" :id="id" @noAddPlan="noAddPlan"></add-plan>
  </div>
</template>
<script>
import addPlan from './addPlan.vue'
import simpleTable from '@/components/table.vue'
import {APIPERSONALFILESTUDENTS, APIPLANDISTRIBUTIONSCHOOLPLANBYSTUDENT} from '@/common/api.js'
export default {
  components: {
    addPlan,
    simpleTable
  },
  props: ['isDistribution', 'uid', 'schoolId'],
  data () {
    return {
      isAddPlan: false,
      id: '',
      form: {
        real_name: ''
      },
      tableConfig: {
        load: false,
        size: 'small', // 表格大小
        style: 'width:100%', // 表格宽度
        align: 'center',
        selection: false,
        border: true,
        config: [
          {name: 'ID', type: 'index', width: '60px'},
          {id: 'plan_title', name: '名称'},
          {id: 'gauge_number', name: '题量'},
          {id: 'guidance_note', name: '简介'}
        ]
      },
      tableData: [],
      plan_type: ''
    }
  },
  created () {
    this.getEdit()
  },
  methods: {
    getEdit () {
      APIPERSONALFILESTUDENTS({
        uid: this.uid
      }).then((res) => {
        this.form = res.data
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    handleClose (done) {
      if (this.tableData.length !== 0) {
        APIPLANDISTRIBUTIONSCHOOLPLANBYSTUDENT({
          range_type: 2,
          plan_type: this.plan_type,
          plan_id: this.tableData[0].id,
          range_object: this.uid,
          school_id: this.schoolId
        }).then((res) => {
          this.$message.success(res.msg)
          this.$emit('noDistribution', {type: 1})
        }).catch((err) => {
          this.$message.error(err.msg)
        })
      }
      this.$emit('noDistribution')
    },
    addPlanFn () {
      this.isAddPlan = true
      this.id = this.schoolId
      this.tableData = []
    },
    noAddPlan (val) {
      this.isAddPlan = false
      if (val.val.length !== 0) {
        this.tableData.push(val.val)
        this.plan_type = val.plan_type
      }
    }
  }
}
</script>
